package net.easyjava.base.manager;

import net.easyjava.framework.web.security.UserSecurityBean;
import net.easyjava.framework.web.security.UserSecurityCookie;
import net.easyjava.tools.Base;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by wxnacy on 15/12/22.
 */
public class BaseManager extends Base {
    private static final long serialVersionUID = -2395234890830211321L;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private UserSecurityCookie userSecurityCookie;

    /**
     * 获取登陆用户id
     * @return
     */
    protected String getUserId(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        UserSecurityBean userSecurityBean  = userSecurityCookie.getUserSecurityBean(request);
        if(userSecurityBean == null){
            logger.info("cookie 中没有登陆用户");
            return null;
        }
        logger.info("登陆用户信息:{}",userSecurityBean.toString());
        return userSecurityBean.getUserId();
    }
}
